Imports cmplib01
Imports System.Windows.Forms

' this form is used for both, Chit Destruction and area objective control

Public Class frmAreaObjectiveProperties
    Inherits System.Windows.Forms.Form

    Const c_Module As String = "frmAreaObjectiveVCProperites"

    Private m_Cancelled As Boolean

    Private ptrGameData As CampaignerGameData
    Private ptrAreaObjectiveVC As AreaObjectiveVictoryCondition
    Private ptrChitDestructionVC As ChitDestructionVictoryCondition
    Private bCancelled As Boolean
    Friend WithEvents btnCancel As System.Windows.Forms.Button

    Private ChitDestructionMode As Boolean = False

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlObjectiveProperties As System.Windows.Forms.Panel
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents cboAreaObjective As System.Windows.Forms.ComboBox
    Friend WithEvents numVPValue As System.Windows.Forms.NumericUpDown
    Friend WithEvents chkAutomaticVictory As System.Windows.Forms.CheckBox
    Friend WithEvents lblVictoryConditionName As System.Windows.Forms.Label
    Friend WithEvents lblAreaObjective As System.Windows.Forms.Label
    Friend WithEvents lblVictoryPointValue As System.Windows.Forms.Label
    Friend WithEvents lblAutomaticVictory As System.Windows.Forms.Label
    Friend WithEvents lblDescription As System.Windows.Forms.Label
    Friend WithEvents txtName As System.Windows.Forms.TextBox
    Friend WithEvents txtDescription As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmAreaObjectiveProperties))
        Me.pnlObjectiveProperties = New System.Windows.Forms.Panel
        Me.lblDescription = New System.Windows.Forms.Label
        Me.lblAutomaticVictory = New System.Windows.Forms.Label
        Me.lblVictoryPointValue = New System.Windows.Forms.Label
        Me.lblAreaObjective = New System.Windows.Forms.Label
        Me.lblVictoryConditionName = New System.Windows.Forms.Label
        Me.txtDescription = New System.Windows.Forms.TextBox
        Me.chkAutomaticVictory = New System.Windows.Forms.CheckBox
        Me.numVPValue = New System.Windows.Forms.NumericUpDown
        Me.cboAreaObjective = New System.Windows.Forms.ComboBox
        Me.txtName = New System.Windows.Forms.TextBox
        Me.btnOK = New System.Windows.Forms.Button
        Me.btnCancel = New System.Windows.Forms.Button
        Me.pnlObjectiveProperties.SuspendLayout()
        CType(Me.numVPValue, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'pnlObjectiveProperties
        '
        Me.pnlObjectiveProperties.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlObjectiveProperties.Controls.Add(Me.lblDescription)
        Me.pnlObjectiveProperties.Controls.Add(Me.lblAutomaticVictory)
        Me.pnlObjectiveProperties.Controls.Add(Me.lblVictoryPointValue)
        Me.pnlObjectiveProperties.Controls.Add(Me.lblAreaObjective)
        Me.pnlObjectiveProperties.Controls.Add(Me.lblVictoryConditionName)
        Me.pnlObjectiveProperties.Controls.Add(Me.txtDescription)
        Me.pnlObjectiveProperties.Controls.Add(Me.chkAutomaticVictory)
        Me.pnlObjectiveProperties.Controls.Add(Me.numVPValue)
        Me.pnlObjectiveProperties.Controls.Add(Me.cboAreaObjective)
        Me.pnlObjectiveProperties.Controls.Add(Me.txtName)
        Me.pnlObjectiveProperties.Location = New System.Drawing.Point(8, 8)
        Me.pnlObjectiveProperties.Name = "pnlObjectiveProperties"
        Me.pnlObjectiveProperties.Size = New System.Drawing.Size(304, 272)
        Me.pnlObjectiveProperties.TabIndex = 0
        '
        'lblDescription
        '
        Me.lblDescription.Location = New System.Drawing.Point(8, 176)
        Me.lblDescription.Name = "lblDescription"
        Me.lblDescription.Size = New System.Drawing.Size(184, 16)
        Me.lblDescription.TabIndex = 9
        Me.lblDescription.Text = "Victory Condition Description"
        '
        'lblAutomaticVictory
        '
        Me.lblAutomaticVictory.Location = New System.Drawing.Point(160, 120)
        Me.lblAutomaticVictory.Name = "lblAutomaticVictory"
        Me.lblAutomaticVictory.Size = New System.Drawing.Size(128, 16)
        Me.lblAutomaticVictory.TabIndex = 8
        Me.lblAutomaticVictory.Text = "Automatic Victory"
        '
        'lblVictoryPointValue
        '
        Me.lblVictoryPointValue.Location = New System.Drawing.Point(8, 120)
        Me.lblVictoryPointValue.Name = "lblVictoryPointValue"
        Me.lblVictoryPointValue.Size = New System.Drawing.Size(128, 16)
        Me.lblVictoryPointValue.TabIndex = 7
        Me.lblVictoryPointValue.Text = "Victory Point Value"
        '
        'lblAreaObjective
        '
        Me.lblAreaObjective.Location = New System.Drawing.Point(8, 55)
        Me.lblAreaObjective.Name = "lblAreaObjective"
        Me.lblAreaObjective.Size = New System.Drawing.Size(280, 30)
        Me.lblAreaObjective.TabIndex = 6
        Me.lblAreaObjective.Text = "Area Objective Target (owned AOs do not show)"
        '
        'lblVictoryConditionName
        '
        Me.lblVictoryConditionName.Location = New System.Drawing.Point(8, 8)
        Me.lblVictoryConditionName.Name = "lblVictoryConditionName"
        Me.lblVictoryConditionName.Size = New System.Drawing.Size(128, 16)
        Me.lblVictoryConditionName.TabIndex = 5
        Me.lblVictoryConditionName.Text = "Victory Condition Name"
        '
        'txtDescription
        '
        Me.txtDescription.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtDescription.Location = New System.Drawing.Point(11, 200)
        Me.txtDescription.Multiline = True
        Me.txtDescription.Name = "txtDescription"
        Me.txtDescription.Size = New System.Drawing.Size(280, 56)
        Me.txtDescription.TabIndex = 4
        Me.txtDescription.Text = "No Victory Condition Description"
        '
        'chkAutomaticVictory
        '
        Me.chkAutomaticVictory.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.chkAutomaticVictory.Location = New System.Drawing.Point(160, 144)
        Me.chkAutomaticVictory.Name = "chkAutomaticVictory"
        Me.chkAutomaticVictory.Size = New System.Drawing.Size(120, 24)
        Me.chkAutomaticVictory.TabIndex = 3
        Me.chkAutomaticVictory.Text = "Automatic Victory"
        '
        'numVPValue
        '
        Me.numVPValue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.numVPValue.Increment = New Decimal(New Integer() {5, 0, 0, 0})
        Me.numVPValue.Location = New System.Drawing.Point(8, 144)
        Me.numVPValue.Minimum = New Decimal(New Integer() {5, 0, 0, 0})
        Me.numVPValue.Name = "numVPValue"
        Me.numVPValue.Size = New System.Drawing.Size(120, 20)
        Me.numVPValue.TabIndex = 2
        Me.numVPValue.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        Me.numVPValue.Value = New Decimal(New Integer() {5, 0, 0, 0})
        '
        'cboAreaObjective
        '
        Me.cboAreaObjective.Location = New System.Drawing.Point(8, 88)
        Me.cboAreaObjective.Name = "cboAreaObjective"
        Me.cboAreaObjective.Size = New System.Drawing.Size(280, 21)
        Me.cboAreaObjective.TabIndex = 1
        Me.cboAreaObjective.Text = "<select an area objective>"
        '
        'txtName
        '
        Me.txtName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtName.Location = New System.Drawing.Point(8, 32)
        Me.txtName.Name = "txtName"
        Me.txtName.Size = New System.Drawing.Size(280, 20)
        Me.txtName.TabIndex = 0
        Me.txtName.Text = "No Victory Condition Name"
        '
        'btnOK
        '
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.Location = New System.Drawing.Point(156, 286)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(75, 23)
        Me.btnOK.TabIndex = 1
        Me.btnOK.Text = "OK"
        '
        'btnCancel
        '
        Me.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnCancel.Location = New System.Drawing.Point(237, 286)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(75, 23)
        Me.btnCancel.TabIndex = 2
        Me.btnCancel.Text = "Cancel"
        '
        'frmAreaObjectiveProperties
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(322, 320)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.pnlObjectiveProperties)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "frmAreaObjectiveProperties"
        Me.ShowInTaskbar = False
        Me.Text = "Area Objective"
        Me.pnlObjectiveProperties.ResumeLayout(False)
        Me.pnlObjectiveProperties.PerformLayout()
        CType(Me.numVPValue, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public ReadOnly Property Cancelled() As Boolean
        Get
            Return bCancelled
        End Get
    End Property

    Public Sub New(ByRef VC As IVictoryCondition, ByRef GameData As CampaignerGameData)
        Me.New()
        Try
            ptrGameData = GameData
            If TypeOf VC Is AreaObjectiveVictoryCondition Then
                ptrAreaObjectiveVC = VC
                ListAvailableAreaObjectives()
            Else
                ptrChitDestructionVC = VC
                ChitDestructionMode = True
                ListAvailableInstallations()
            End If
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

        PopulateProperties()

    End Sub

    Private Sub ListAvailableAreaObjectives()
        Dim ao As AreaObjective
        Try
            cboAreaObjective.Items.Clear()

            For Each ao In ptrGameData.AreaObjectives
                ' add each area objective, except those that are already owned.
                If ao.OwningFaction <> ptrGameData.selectedFaction.FactionID Then
                    ' add it
                    cboAreaObjective.Items.Add(ao)
                End If
            Next
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub ListAvailableInstallations()
        Dim gcKey As String
        Dim gc As GroundChitType

        Try
            cboAreaObjective.Items.Clear()

            Dim mem As ChitMember
            For Each mem In ptrGameData.Chits
                If mem.UnitType = GroundChitClass_cgd.Objective Then
                    '// list it baby (if its not the same faction)
                    If mem.OwningFaction.FactionID <> ptrGameData.selectedFaction.FactionID Then cboAreaObjective.Items.Add(mem)
                End If
            Next

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub PopulateProperties()
        Dim VC As IVictoryCondition

        If ChitDestructionMode = False Then
            VC = ptrAreaObjectiveVC
        Else
            VC = ptrChitDestructionVC
        End If

        Try
            txtName.Text = VC.VictoryConditionName
            txtDescription.Text = VC.Description
            numVPValue.Value = VC.VPValue
            chkAutomaticVictory.Checked = VC.AutomaticVictory

            If ChitDestructionMode = False Then
                ' if the Area Objective is set, select it from the list of available objectives
                If ptrAreaObjectiveVC.TargetAreaObjective Is Nothing Then
                    ' ok... we don't need to do anything.
                Else
                    cboAreaObjective.SelectedItem = ptrAreaObjectiveVC.TargetAreaObjective
                End If
            Else
                Me.Text = "Chit Destruction"
                lblAreaObjective.Text = "Select an installation to destroy (owned installations do not show)"
                If ptrChitDestructionVC.ChitToDestroy Is Nothing Then
                    cboAreaObjective.Text = "<select a target installation>"
                Else
                    cboAreaObjective.SelectedItem = ptrChitDestructionVC.ChitToDestroy
                End If
            End If
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Function SaveProperties() As Boolean
        Dim ptrIVC As IVictoryCondition

        Try
            If ChitDestructionMode = False Then
                ptrIVC = ptrAreaObjectiveVC
            Else
                ptrIVC = ptrChitDestructionVC
            End If

            ptrIVC.VictoryConditionName = txtName.Text
            ptrIVC.Description = txtDescription.Text
            ptrIVC.VPValue = numVPValue.Value
            ptrIVC.AutomaticVictory = chkAutomaticVictory.Checked

            If ChitDestructionMode = False Then
                If cboAreaObjective.SelectedItem Is Nothing Then
                    Return False
                Else
                    ptrAreaObjectiveVC.TargetAreaObjective = cboAreaObjective.SelectedItem
                End If
            Else
                If cboAreaObjective.SelectedItem Is Nothing Then
                    Return False
                Else
                    ptrChitDestructionVC.ChitToDestroy = cboAreaObjective.SelectedItem
                End If
            End If

        Catch ex As Exception
            HandleException(c_Module, ex)
            Return False
        End Try

        Return True
    End Function

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Try
            If SaveProperties() = True Then
                Me.Hide()
            Else
                If ChitDestructionMode = False Then
                    MessageBox.Show("You must select an Area Objective for this Victory Condition", "Select AO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Else
                    MessageBox.Show("You must select a target installation for this Victory Condition", "Select Installation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        bCancelled = True
        Me.Hide()
    End Sub
End Class
